package com.ruoyi.web.controller.module.event;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.estate.Estate;
import com.ruoyi.system.domain.module.event.EventPlan;
import com.ruoyi.system.domain.module.event.EventPlanWork;
import com.ruoyi.system.domain.module.property.Property;
import com.ruoyi.system.service.module.estate.IEstateService;
import com.ruoyi.system.service.module.event.IEventPlanService;
import com.ruoyi.system.service.module.event.IEventPlanWorkService;
import com.ruoyi.system.service.module.property.IPropertyService;
import com.ruoyi.system.service.system.ISysUserService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 计划的任务：记录计划的状态和完成人的信息Controller
 *
 * @author ruoyi
 * @date 2023-12-28
 */
@Validated
@Api(value = "计划的任务：记录计划的状态和完成人的信息控制器", tags = {"计划的任务：记录计划的状态和完成人的信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/event/work")
public class EventPlanWorkController extends BaseController {

    private final IEventPlanWorkService eventPlanWorkService;
	private final IPropertyService propertyService;
	private final IEstateService estateService;
	private final IEventPlanService eventPlanService;
	private final ISysUserService sysUserService;


    /**
     * 查询计划的任务：记录计划的状态和完成人的信息列表
     */
    @ApiOperation("查询计划的任务：记录计划的状态和完成人的信息列表")
    @PreAuthorize("@ss.hasPermi('module:work:list')")
    @GetMapping("/list")
    public TableDataInfo<EventPlanWork> list(@Validated EventPlanWork eventPlanWork) {
        QueryWrapper<EventPlanWork> qw = new QueryWrapper<>();
		if (eventPlanWork.getEventPlanId()!=null){
			qw.eq("event_plan_id",eventPlanWork.getEventPlanId());
		}
		if (eventPlanWork.getState()!=null){
			qw.eq("state",eventPlanWork.getState());
		}else {
			qw.eq("state",1);
		}
        TableDataInfo<EventPlanWork> td = PageUtils.buildDataInfo(eventPlanWorkService.page(PageUtils.buildPage(),qw));
        List<EventPlanWork> list = td.getRows();
		for (EventPlanWork list1:list){
			if (list1.getWorkerSysUserId()!=null){
				SysUser user = sysUserService.selectUserById(list1.getWorkerSysUserId());
				if (user!=null){
					list1.setNickName(user.getNickName());
				}
			}else {
				list1.setNickName("未执行");
			}
			if (list1.getPropertyId()!=null){
				Property property = propertyService.findById(list1.getPropertyId());
				if (property!=null){
					list1.setPropertyName(property.getPropertyName());
				}
			}
			if (list1.getEstateId()!=null){
				Estate estate = estateService.findById(list1.getEstateId());
				if (estate!=null){
					list1.setEstateName(estate.getEstateName());
				}
			}
			if (list1.getEventPlanId()!=null){
				EventPlan eventPlan = eventPlanService.findById(list1.getEventPlanId());
				if (eventPlan!=null){
					list1.setPlanName(eventPlan.getPlanTitle());
				}
			}
			if (list1.getOpResult()==null){
				list1.setOpResult("暂无");
			}
		}
        return td;
    }

    /**
     * 导出计划的任务：记录计划的状态和完成人的信息列表
     */
    @ApiOperation("导出计划的任务：记录计划的状态和完成人的信息列表")
    @PreAuthorize("@ss.hasPermi('module:work:export')")
    @Log(title = "计划的任务：记录计划的状态和完成人的信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EventPlanWork> export(@Validated EventPlanWork eventPlanWork) {
        List<EventPlanWork> list = eventPlanWorkService.list();
        ExcelUtil<EventPlanWork> util = new ExcelUtil<EventPlanWork>(EventPlanWork.class);
        return util.exportExcel(list, "计划的任务：记录计划的状态和完成人的信息");
    }

    /**
     * 获取计划的任务：记录计划的状态和完成人的信息详细信息
     */
    @ApiOperation("获取计划的任务：记录计划的状态和完成人的信息详细信息")
    @PreAuthorize("@ss.hasPermi('module:work:query')")
    @GetMapping("/{id}")
    public AjaxResult<EventPlanWork> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        EventPlanWork eventPlanWork = eventPlanWorkService.getById(id);
        return AjaxResult.success(eventPlanWork);
    }

    /**
     * 新增计划的任务：记录计划的状态和完成人的信息
     */
    @ApiOperation("新增计划的任务：记录计划的状态和完成人的信息")
    @PreAuthorize("@ss.hasPermi('module:work:add')")
    @Log(title = "计划的任务：记录计划的状态和完成人的信息", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EventPlanWork eventPlanWork) {
        return toAjax(eventPlanWorkService.save(eventPlanWork) ? 1 : 0);
    }

    /**
     * 修改计划的任务：记录计划的状态和完成人的信息
     */
    @ApiOperation("修改计划的任务：记录计划的状态和完成人的信息")
    @PreAuthorize("@ss.hasPermi('module:work:edit')")
    @Log(title = "计划的任务：记录计划的状态和完成人的信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EventPlanWork eventPlanWork) {
        return toAjax(eventPlanWorkService.updateById(eventPlanWork) ? 1 : 0);
    }

    /**
     * 删除计划的任务：记录计划的状态和完成人的信息
     */
    @ApiOperation("删除计划的任务：记录计划的状态和完成人的信息")
    @PreAuthorize("@ss.hasPermi('module:work:remove')")
    @Log(title = "计划的任务：记录计划的状态和完成人的信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(eventPlanWorkService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
